﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Timer
{
    public partial class frmMain : Form
    {
        private XMLHelper CC = new XMLHelper();
        private DateTime _dtTotalTime = DateTime.MinValue;
        private DateTime _dtAlertTime = DateTime.MinValue;
        private DateTime _dtFinishTime = DateTime.MinValue;
        private IList<SectionEntity> _sections;
        private IList<FontSizeEntity> _fonts;
        private bool _isClickStop = false;
        private string _alertSoundFileFullName = string.Empty;
        private string _finishSoundFileFullName = string.Empty;


        public frmMain()
        {
            InitializeComponent();
        }

        #region Custom methods

        /// <summary>
        /// Bind sections
        /// </summary>
        private void BindSections()
        {
            this._sections = CC.ParseXmlForSection();
            this._fonts = CC.ParseXmlForFontSize();

            // Bind Sections.
            this.cbxSections.DataSource = this._sections;
            //this.cbxSections.ValueMember = "ID";
            this.cbxSections.DisplayMember = "SectionName";
        }

        private void InitiaFont()
        {
            FontSizeEntity fontEntity = this._fonts[0];
            this.labSectionTitle.Font = new Font("黑体", fontEntity.TitleFontSize, FontStyle.Regular);
            this.labTime.Font = new Font("黑体", fontEntity.TimeFontSize, FontStyle.Regular);
        }

        /// <summary>
        /// Initia section for selected.
        /// </summary>
        private void IntialSection()
        {
            SectionEntity sectEnt = this.cbxSections.SelectedItem as SectionEntity;
            if (sectEnt == null)
                return;

            
            this.labSectionTitle.Text = sectEnt.SectionTitle;

            // Set timer control to Enabled. 

            this._dtTotalTime = DateTime.Parse(
                string.Format("{0} {1}",
                    DateTime.Now.ToString("yy-MM-dd"),
                    sectEnt.TotalTime)
                );
            this._dtAlertTime = DateTime.Parse(
                string.Format("{0} {1}",
                    DateTime.Now.ToString("yy-MM-dd"),
                    sectEnt.AlertTime)
                );
            this._dtFinishTime = DateTime.Parse(
                string.Format("{0} 00:00:00", DateTime.Now.ToString("yy-MM-dd"))
                );


            this.labTime.Text = string.Format("倒计时 {0}", _dtTotalTime.ToString("T"));

            // Set alert file and finish full name
            this._alertSoundFileFullName = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, sectEnt.AlertSoundFile);
            this._finishSoundFileFullName = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, sectEnt.FinishSoundFile);
        }

        /// <summary>
        /// Set button status
        /// </summary>
        /// <param name="status">
        /// 1: Stop Timer
        /// 2: Suspend Timer
        /// 3: Start Timer
        /// </param>
        private void SetButtonStatus(int status)
        {
            switch (status)
            {
                case 1:
                    this.btnStart.Enabled = true;
                    this.btnStop.Enabled = false;
                    this.btnSuspend.Enabled = false;
                    this.btnReset.Enabled = true;
                    this.btnConfig.Enabled = true;
                    break;
                case 2:
                    this.btnStart.Enabled = true;
                    this.btnStop.Enabled = true;
                    this.btnSuspend.Enabled = false;
                    this.btnReset.Enabled = false;
                    this.btnConfig.Enabled = false;
                    break;
                case 3:
                    this.btnStart.Enabled = false;
                    this.btnStop.Enabled = true;
                    this.btnSuspend.Enabled = true;
                    this.btnReset.Enabled = false;
                    this.btnConfig.Enabled = false;
                    break;
            }
        }

        /// <summary>
        /// Play alert or finish .wav sound file.
        /// </summary>
        /// <param name="wavFileName"></param>
        private void SoundPlay(string wavFileName)
        {
            using (System.Media.SoundPlayer soundPlayer = new System.Media.SoundPlayer(wavFileName))
            {
                if (System.IO.File.Exists(wavFileName))
                {
                    try
                    {
                        soundPlayer.Play();
                    }
                    catch
                    {}
                }
            }
        }

        #endregion Custom methods


        #region Control events
        /// <summary>
        /// timer Tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            this._dtTotalTime = this._dtTotalTime.AddSeconds(-1);
            this.labTime.Text = string.Format("倒计时 {0}", _dtTotalTime.ToString("T"));

            if (DateTime.Compare(this._dtTotalTime, this._dtAlertTime) == 0)
            {
                // Play alert sound.
                this.SoundPlay(this._alertSoundFileFullName);
            }

            // If the time is 00:00:00 then disable timer.
            if (DateTime.Compare(this._dtTotalTime, this._dtFinishTime) == 0)
            {
                this.btnStop_Click(this.btnStop, EventArgs.Empty);
                // play finish sound.
                this.SoundPlay(this._finishSoundFileFullName);
            }

        }

        /// <summary>
        /// Form load.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Top = 0;
            this.Left = 0;
            Application.CurrentCulture = new System.Globalization.CultureInfo("zh-CN", false);
            
            this.BindSections();
            this.InitiaFont();

            this.cbxSections.SelectedIndex = 0;
            this.cbxSections_SelectedIndexChanged(this.cbxSections, EventArgs.Empty);

            // Set button status for stop timer.
            this.SetButtonStatus(1);
        }

        /// <summary>
        /// Changed combobox select item.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxSections_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.IntialSection();
        }

        /// <summary>
        /// Timing begins when click start button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (this._isClickStop)
                this.IntialSection();

            this._isClickStop = false;

            // Set button status for start timer.
            this.SetButtonStatus(3);

            // Start timer
            this.timer1.Enabled = true;
        }

        /// <summary>
        /// Suspend timer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSuspend_Click(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            // Set button status for suspend timer.
            this.SetButtonStatus(2);
        }

        /// <summary>
        /// Stop timer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            this._isClickStop = true;

            // Set button status for stop timer.
            this.SetButtonStatus(1);
        }

        /// <summary>
        /// Reset timer.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            this.IntialSection();
        }

        /// <summary>
        /// Occurs when click config button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfig_Click(object sender, EventArgs e)
        {
            frmSectionSetting SectionSetting = new frmSectionSetting(this._sections, this._fonts, CC);
            SectionSetting.Top = 0;
            SectionSetting.Left = Screen.PrimaryScreen.Bounds.Width / 2 - SectionSetting.Width / 2;
            DialogResult disRes = SectionSetting.ShowDialog();

            if (disRes == System.Windows.Forms.DialogResult.OK)
            {
                // Re-bind section
                int index = this.cbxSections.SelectedIndex;
                this.BindSections();
                this.InitiaFont();
                this.cbxSections.SelectedIndex = index;
                this.cbxSections_SelectedIndexChanged(this.cbxSections, EventArgs.Empty);
            }
        }

        #endregion Control Event.
    }
}
